name: CI - E2E - Web browser
on:
  workflow_dispatch:
  workflow_run:
    workflows: ['CI - Node.js']
    types:
      - completed
  # TODO: refactor with a workflow_call
  pull_request:
    paths-ignore:
      - 'docs/**'
    branches:
      - 'main'
      - 'next-**'
      - 'e2e-**'

jobs:
  build-client:
    name: Build Client
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        node-version: [20.x]

    steps:
      - name: Checkout Source Files
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

      - name: Checkout client-config
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
        with:
          repository: freeCodeCamp/client-config
          path: client-config

      - name: Setup pnpm
        uses: pnpm/action-setup@a3252b78c470c02df07e9d59298aecedc3ccdd6d #v3.0.0
        with:
          version: 8

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
        with:
          node-version: ${{ matrix.node-version }}
          cache: pnpm

      - name: Set freeCodeCamp Environment Variables
        run: cp sample.env .env

      - name: Install and Build
        run: |
          pnpm install
          pnpm run build

      - name: Move serve.json to Public Folder
        run: cp client-config/serve.json client/public/serve.json

      # We tar them for performance reasons - uploading a lot of files is slow.
      - name: Tar Files
        run: tar -cf client-artifact.tar client/public

      - name: Upload Client Artifact
        uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
        with:
          name: client-artifact
          path: client-artifact.tar

      - name: Upload Webpack Stats
        uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
        with:
          name: webpack-stats
          path: client/public/stats.json

  cypress-run:
    name: Test
    runs-on: ubuntu-22.04
    needs: build-client
    strategy:
      fail-fast: false
      matrix:
        browsers: [chrome, firefox, electron]
        node-version: [20.x]
        include:
          - browsers: electron
            spec: cypress/e2e/default/learn/challenges/projects.ts

    services:
      mongodb:
        image: mongo:4.4
        ports:
          - 27017:27017
      # We need mailhog to catch any emails the api tries to send.
      mailhog:
        image: mailhog/mailhog
        ports:
          - 1025:1025

    steps:
      - name: Set Action Environment Variables
        run: |
          echo "CYPRESS_RECORD_KEY=${{ secrets.CYPRESS_RECORD_KEY }}" >> $GITHUB_ENV
          echo "GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" >> $GITHUB_ENV

      - name: Checkout Source Files
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

      - uses: actions/download-artifact@c850b930e6ba138125429b7e5c93fc707a7f8427 # v4.1.4
        with:
          name: client-artifact

      - name: Unpack Client Artifact
        run: |
          tar -xf client-artifact.tar
          rm client-artifact.tar

      - name: Downgrade Firefox
        run: |
          curl https://ftp.mozilla.org/pub/firefox/releases/123.0/linux-x86_64/en-US/firefox-123.0.tar.bz2 --output firefox-123.0.tar.bz2
          tar -xjf firefox-123.0.tar.bz2
          sudo mv firefox /opt/
          sudo mv /usr/bin/firefox /usr/bin/firefox_old
          sudo ln -s /opt/firefox/firefox /usr/bin/firefox

      - name: Setup pnpm
        uses: pnpm/action-setup@a3252b78c470c02df07e9d59298aecedc3ccdd6d #v3.0.0
        with:
          version: 8

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
        with:
          node-version: ${{ matrix.node-version }}
          # cypress-io/github-action caches the store, so we should not cache it
          # here.

      - name: Set freeCodeCamp Environment Variables
        run: cp sample.env .env

      - name: Install and Build
        run: |
          pnpm install
          pnpm run create:shared
          pnpm run build:curriculum
          pnpm run build:server

      - name: Seed Database
        run: pnpm run seed

      # start-ci uses pm2, so it needs to be installed globally
      - name: Install pm2
        run: npm i -g pm2

      - name: Cypress run
        uses: cypress-io/github-action@v6
        with:
          record: ${{ env.CYPRESS_RECORD_KEY != 0 }}
          start: pnpm run start-ci
          wait-on: http://localhost:8000
          wait-on-timeout: 1200
          config: baseUrl=http://localhost:8000
          browser: ${{ matrix.browsers }}
          spec: ${{ matrix.spec }}
